/*
 * Copyright (C) 2020 UNISOC Open Source Project
 *
 */
package com.unisoc.systemui.qs.tiles;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.service.quicksettings.Tile;
import android.graphics.drawable.Drawable;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.os.Handler;
import android.os.Looper;
import android.os.RemoteException;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.RelativeSizeSpan;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnAttachStateChangeListener;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Checkable;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.Switch;
import android.widget.TextView;
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
import com.android.settingslib.graph.BatteryMeterDrawableBase;
import com.android.systemui.R;
import com.android.systemui.plugins.qs.DetailAdapter;
import com.android.systemui.plugins.qs.QSTile.BooleanState;
import com.android.systemui.qs.QSHost;
import com.android.systemui.qs.tileimpl.QSTileImpl;
import com.android.systemui.Dependency;
import com.unisoc.systemui.superpower.UnisocPowerManagerUtil;
import java.text.NumberFormat;
import android.os.BatteryManager;
import android.provider.Settings;

import javax.inject.Inject;

/* UNISOC: Bug 1074234 Super power feature */
public class SuperBatteryTile extends QSTileImpl<BooleanState> {

    private boolean mIsChargeExitLow;

    @Inject
    public SuperBatteryTile(QSHost host) {
        super(host);
    }

    @Override
    public BooleanState newTileState() {
        return new BooleanState();
    }

    @Override
    public int getMetricsCategory() {
        return MetricsEvent.QS_BATTERY_TILE;
    }

    @Override
    public Intent getLongClickIntent() {
        return new Intent(Intent.ACTION_POWER_USAGE_SUMMARY);
    }

    @Override
    protected void handleClick() {
        if (mIsChargeExitLow && isCharging()) {
            return;
        }
        setBatterySaveMode();
    }

    @Override
    protected void handleLongClick() {
        if (UnisocPowerManagerUtil.isSuperPower()) {
            return;
        }
        super.handleLongClick();
    }

    @Override
    public CharSequence getTileLabel() {
        return mContext.getString(getTileLabelResId());
    }

    private int getTileLabelResId() {
        return R.string.super_battery_detail_switch_title;
    }

    private void setBatterySaveMode() {
        if (!UnisocPowerManagerUtil.isSuperPower()) {
            UnisocPowerManagerUtil.setPowerSaveMode(UnisocPowerManagerUtil.MODE_ULTRASAVING);
            if (mHost != null) {
                mHost.collapsePanelsForPowerSave();
            }
        }

    }
    @Override
    protected void handleUpdateState(BooleanState state, Object arg) {
        state.icon = ResourceIcon.get(R.drawable.ic_qs_battery_super_save);
        state.label = mContext.getString(R.string.super_battery_detail_switch_title);
        state.contentDescription = state.label;

        mIsChargeExitLow = UnisocPowerManagerUtil.getSmartSavingModeWhenCharging();
        Log.d(TAG, "mIsChargeExitLow= " + mIsChargeExitLow);

        state.state = (mIsChargeExitLow && isCharging()) ? Tile.STATE_UNAVAILABLE : Tile.STATE_INACTIVE;
    }

    public boolean isCharging() {
           int usb_plugin = Settings.Global.getInt(mContext.getContentResolver(),
                            "usb_plugin", 0);
           //Log.d(TAG, "isCharging=" + usb_plugin); // UNISOC: Modify for bug1356301
           return (usb_plugin == 1);
    }

}
